package com.itheima.test4;


import java.util.Random;

public class MyThread extends Thread {

    //总金额
    static double money = 100;
    //红包的个数
    static int count = 3;
    //最小的红包
    static final double MIN = 0.01;

    @Override
    public void run() {
        //同步代码块
        synchronized (MyThread.class){
            if(count == 0){
                //判断，共享数据是否到了末尾（到了末尾）
                System.out.println("红包已被抢光");
            }else{
                //判断，共享数据是否到了末尾（没到末尾）
                //定义一个变量，表示中奖的金额
                double prize = 0;
                if(count == 1){
                    //表示是最后的红包
                    prize = money;
                }else {
                    //表示第一次第二次（随机）
                    Random r = new Random();
                    double bounds = money - (count - 1) * MIN;
                    //prize = r.nextDouble(bounds);
                    prize = bounds * r.nextDouble();
                    if(prize < MIN){
                        prize = MIN;
                    }
                }

                //从money当中，去掉当前中奖的金额
                money = money - prize;
                //减掉一个红包
                count--;
                //输出结果
                System.out.println(getName()+"抢到了"+prize+"元");

            }
        }
    }
}
